/*
 * IPhisicsObject.cpp
 *
 *  Created on: 03.03.2012
 *      Author: Kirill Blinov
 *      Copyright: LGPL
 */

#include "IPhysicsObject.h"
#include "../CDynamicEngine.h"

static IPhysicsObject*
PhysicObjectFabric::CreatePhysicsObject( const float Mass, glm::quat  Orientation, glm::vec3 Position,
                                         const glm::vec3 Box[2], const glm::vec3 Inertia )
{
    btCollisionShape     *shape;
    btMotionState        *motionState;
    glm::vec3 Sizes = ( Box[0] - Box[1] ) / 2.0f;
    shape = new btBoxShape( btVector3( abs(Sizes.x), abs(Sizes.y), abs(Sizes.z) ) );

    btTransform trans;
    trans.setIdentity();

    btQuaternion qtn;
    glm::vec3 EulerAngles = glm::eulerAngles( Orientation );
    qtn.setEuler( EulerAngles.x, EulerAngles.y , EulerAngles.z );

    trans.setRotation( qtn );
    trans.setOrigin( btVector3( Position.x, Position.y, Position.z ) );
    motionState = new btDefaultMotionState( trans );

    btVector3 TmpInertia( Inertia.x, Inertia.y, Inertia.z );
    if ( ( 0.0f == Inertia.x ) &&   ( 0.0f == Inertia.y ) &&  ( 0.0f == Inertia.z ) )
    {
            shape->calculateLocalInertia( Mass, TmpInertia);
    }

    btRigidBody::btRigidBodyConstructionInfo RigidBodyConstructionInfo(  btScalar(Mass), motionState, shape, TmpInertia );
    RigidBodyConstructionInfo.m_restitution = 0.01f;//TODO receive this data as input parameters
    RigidBodyConstructionInfo.m_friction = 0.8f;
    RigidBodyConstructionInfo.m_linearDamping = 0.2;
    RigidBodyConstructionInfo.m_angularDamping = 0.1f;

    btRigidBody *NewBody = new btRigidBody( RigidBodyConstructionInfo );

	return new CRigidConvexPhisicsObject( NewBody );
}

static void
PhysicObjectFabric::DeletePhysicsObject( IPhysicsObject* Object )
{
	CDynamicEngine *Engine = CDynamicEngine::GetEngine();
	Engine->UnregisterPhysicsObject( *Object );
	delete Object;

}

//----------------------------------//

IPhysicsObject::IPhysicsObject()
{

}

IPhysicsObject::~IPhysicsObject()
{

}

